//JETNUKE

/*
 * Copyright (c) 2004-2006 by Andrea Mazzolini [andreamazzolini@users.sourceforge.net]
 * http://www.jetnuke.org
 * 
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307, USA.
 *  
 */

package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.HeadLine;

public class GenericHeadLinesDAO extends AutoKeys implements
		org.jetnuke.dao.HeadLinesDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericHeadLinesDAO.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.HeadLinesModel#selectAll()
	 */
	public List selectAll() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAll() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("HeadLinesModel.selectAll"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAll() - end"); //$NON-NLS-1$
		}
		return list;
	}

	/**
	 * @param rs
	 * @return
	 */
	protected List processSelectAll(ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - start"); //$NON-NLS-1$
		}

		List list = new ArrayList();
		while (rs.next()) {
			HeadLine u = new HeadLine();
			fillHeadLineFromResultSet(u, rs);
			list.add(u);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/**
	 * @param u
	 * @param rs
	 */
	protected void fillHeadLineFromResultSet(HeadLine u, ResultSet rs)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("fillHeadLineFromResultSet(HeadLine, ResultSet) - start"); //$NON-NLS-1$
		}

		u.setHEADLINESURL(rs.getString("HEADLINESURL"));
		u.setHID(rs.getInt("HID"));
		u.setSITENAME(rs.getString("SITENAME"));

		if (logger.isDebugEnabled()) {
			logger
					.debug("fillHeadLineFromResultSet(HeadLine, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.HeadLinesModel#retrieveByPK(int)
	 */
	public HeadLine retrieveByPK(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("HeadLinesModel.selectByID"));
		p.setInt(1, id);
		ResultSet rs = p.executeQuery();
		HeadLine s = new HeadLine();

		if (rs.next()) {
			fillHeadLineFromResultSet(s, rs);
		}

		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - end"); //$NON-NLS-1$
		}
		return s;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.HeadLinesModel#deleteByPK(int)
	 */
	public void deleteByPK(int i) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("deleteByPK(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("HeadLinesModel.deleteByID"));
		p.setInt(1, i);
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("deleteByPK(int) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.HeadLinesModel#update(org.jetnuke.entities.HeadLine)
	 */
	public void update(HeadLine hl) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("update(HeadLine) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(SystemGlobals.getSql("HeadLinesModel.update"));
		p.setString(1, hl.getHEADLINESURL());
		p.setString(2, hl.getSITENAME());
		p.setInt(3, hl.getHID());
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("update(HeadLine) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.HeadLinesModel#addNew(org.jetnuke.entities.HeadLine)
	 */
	public int addNew(HeadLine hl) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("addNew(HeadLine) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this
				.getStatementForAutoKeys("HeadLinesModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals
				.getSql("HeadLinesModel.lastGeneratedHeadlineID"));
		initHeadline(hl, p);

		int id = this.executeAutoKeysQuery(p);
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("addNew(HeadLine) - end"); //$NON-NLS-1$
		}
		return id;

	}

	/**
	 * @param hl
	 * @param p
	 */
	protected void initHeadline(HeadLine hl, PreparedStatement p)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("initHeadline(HeadLine, PreparedStatement) - start"); //$NON-NLS-1$
		}

		p.setString(1, hl.getHEADLINESURL());
		p.setString(2, hl.getSITENAME());

		if (logger.isDebugEnabled()) {
			logger.debug("initHeadline(HeadLine, PreparedStatement) - end"); //$NON-NLS-1$
		}
	}

}
